package org.standard;
import java.awt.Image;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;





public class Map {
	private int[][] mapArray = null;

	private int size = 0;

	private int maxX = 0;

	private int maxY = 0;

	private int tile = 32;

	private Image[] mapImages;
	public Map(final String fileName, final int tile) {
		try {
			this.mapArray = Map.loadArray(fileName);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
		this.size = this.mapArray.length;
		this.tile = tile;
		this.maxX = this.size;
		this.maxY = (((int[]) this.mapArray[this.maxX > 0 ? this.maxX - 1 : 0]).length);
		//this.mapImages = Utility.getSplitImages("image/map.png", tile, tile);
	}
	public static int[][] loadArray(final String fileName) throws IOException {		
		List list = loadList(fileName);
		int row = list.size();
		int[][] mapArray = new int[row][];
		for (int i = 0; i < row; i++) {
			mapArray[i] = (int[]) list.get(i);
		}
		int col = (((int[]) mapArray[row > 0 ? row - 1 : 0]).length);
		int[][] result = new int[col][row];
		for (int j = 0; j < col; j++) {
			for (int i = 0; i < row; i++) {
				result[i][j] = mapArray[j][i];
			}
		}
		return result;
	}
	private static List loadList(final String fileName) throws IOException {
		BufferedReader reader = new BufferedReader(new InputStreamReader(
				Utility.getResource(fileName)));
		List records = new ArrayList(10);
		String result = null;
		try {
			while ((result = reader.readLine()) != null) {
				char[] charArray = result.toCharArray();
				int size = charArray.length;
				int[] intArray = new int[size];
				for (int i = 0; i < size; i++) {
					intArray[i] = Character.getNumericValue(charArray[i]);
				}
				records.add(intArray);
			}
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e) {
				}
			}
		}
		return records;
	}
}
